Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
How to save parent.child name with looping through a nested object in JavaScript? like {parent.child : "value"}

I have an object as an input with a structure like this:(basically a simple nested object)

const test ={
  id: "0000",
  parent1 : "foo",
  parent2: { child1: "data1",
             child2: "data2",
             child3: "data3" 
           },
  parent3: { child1: "data1",
             child2: "data2",
             child3:  "data3"
           } 
}

This test object has some empty values, and the purpose of the code is to loop through the object and find properties with values.

const update = {};
  for(const key in test){
    if(test[key] !== ""){
      update[key] = test[key];
      console.log(update);
    }
  };

The loop works just fine finding them. But when it finds a child, it saves it like this, as an example, {child: "data2"} and won't save the parent's name.

How is possible to modify this code and avoid too many if/else conditions, basically an optimized code, when having an object like this:

{
  id: "foo",
  parent1 : Null,
  parent2: { child1: Null,
             child2: "data2",
             child3: Null
           },
  parent3: { child1: Null,
             child2: Null,
             child3:  "data3"
           } 
}

A result like this:

{ id: "foo", parent2.child2: "data2", parent3.child3: "data3"]

or more likely something like this:

{ id: "foo", parent2:{child2: "data2"}, parent3:{child3: "data3"}}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!